<template>
  <view class="result-container">
    <e-navbar leftTitle="测算结果"></e-navbar>
    <u-gap height="10"></u-gap>
    <view class="result-top">
      <image :src="$image('/images/result-top-bg.png')" mode="aspectFill"></image>
      <view class="result-content">
        <view class="result-content_row m-b-18">
          <view class="result-top_title font-28" v-if="userinfo">{{userinfo.nickname || ''}}</view>
          <view class="result-top_value flex col-center row-between">
            <!--<u-icon name="edit-pen-fill" color="#fff" size="20"></u-icon>-->
            <view v-if="augurDate.gender">{{augurDate.gender || ''}}</view>
          </view>
        </view>
        <view class="result-content_row m-b-10">
          <view class="result-top_title">公历</view>
          <view class="result-top_value flex col-center row-between">
            <view>{{ augurDate.solarDate }}</view>
          </view>
        </view>
        <view class="result-content_row m-b-10">
          <view class="result-top_title">农历</view>
          <view class="result-top_value flex col-center row-between">
            <view>{{ augurDate.lunarDate }}</view>
          </view>
        </view>
        <view class="result-content_row m-b-10">
          <view class="result-top_title">八字</view>
          <view class="result-top_value flex col-center" style="justify-content: flex-start">
            <view class="m-r-30" v-for="(item,index) in augurDate.shiShen" :key="index">{{item}}</view>
          </view>
        </view>
      </view>
    </view>
    <u-gap height="15"></u-gap>
    <view class="result-body">
      <view class="result-body_cell">
        <view class="result-body_cell_title">日期</view>
        <view class="result-body_cell_value">
          <view v-for="(item,index) in timeList" :key="index">{{item}}</view>
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">十神</view>
        <view class="result-body_cell_value">
          <view v-for="(item,index) in augurDate.shiShen" :key="index">{{item}}</view>
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">天干</view>
        <view class="result-body_cell_value">
          <view style="font-size: 48rpx" :class="[typeColor(item.type)]" v-for="(item,index) in augurDate.tianGan" :key="index">{{item.title}}</view>
          <!--<view class="pingfang-heavy" :class="[typeColor(item)]" v-for="(item,index) in tiangan" :key="index">{{item}}</view>-->
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">地支</view>
        <view class="result-body_cell_value">
          <view style="font-size: 48rpx" :class="[typeColor(item.type)]" v-for="(item,index) in augurDate.diZhi" :key="index">{{item.title}}</view>
          <!--<view class="pingfang-heavy" :class="[typeColor(item)]" v-for="(item,index) in dizhih" :key="index">{{item}}</view>-->
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">藏干</view>
        <view class="result-body_cell_value" style="align-items: flex-start">
          <view v-for="(item,index) in augurDate.zangGan" :key="index">
            <view class="m-b-6" v-for="(citem,cindex) in item" :key="cindex">
              <text class="m-r-10" :class="[zangganColor(citem.slice(0,1))]">{{citem.slice(0,1)}}</text>
              <text>{{citem.slice(1)}}</text>
            </view>
          </view>
          <!--<view v-for="(item,index) in zanggan" :key="index">-->
          <!--  <view class="m-b-6" v-for="(citem,cindex) in item" :key="cindex">-->
          <!--    <text class="m-r-10" :class="[typeColor(citem.slice(0,1))]">{{citem.slice(0,1)}}</text>-->
          <!--    <text>{{citem.slice(1)}}</text>-->
          <!--  </view>-->
          <!--</view>-->
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">地势</view>
        <view class="result-body_cell_value">
          <view class="" v-for="(item,index) in augurDate.diShi" :key="index">{{item}}</view>
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">空亡</view>
        <view class="result-body_cell_value">
          <view class="" v-for="(item,index) in augurDate.kongWang" :key="index">{{item}}</view>
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">纳音</view>
        <view class="result-body_cell_value">
          <view class="" v-for="(item,index) in augurDate.naYin" :key="index">{{item}}</view>
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title">神煞</view>
        <view class="result-body_cell_value" style="align-items: flex-start">
          <!--<view v-for="(item,index) in shensha" :key="index">-->
          <!--  <view class="m-b-6" v-for="(citem,cindex) in item" :key="cindex">-->
          <!--    <text>{{citem}}</text>-->
          <!--  </view>-->
          <!--</view>-->
          <view class=" m-b-4" v-for="(item,index) in augurDate.jiShenXiongSha" :key="index">
            <view class="m-r-16" v-for="(citem, cindex) in item" :key="cindex">{{ citem }}</view>
          </view>
        </view>
      </view>
      <view class="result-body_cell">
        <view class="result-body_cell_title text-center flex-col col-center p-t-6 p-b-6" style="justify-content: space-between">
          <view class="time-text">起始</view>
          <view class="p-l-10 p-r-10 font-38 pingfang-heavy " style="line-height: 60rpx;width: 60rpx">流年</view>
          <view class="time-text">起始</view>
        </view>
        <!--<view class="liunian-content" v-for="(item,index) in liunian" :key="index">-->
        <!--  <view class="time-text">{{item.year1}}</view>-->
        <!--  <view v-for="(citem,cindex) in item.list" :key="cindex">{{citem}}</view>-->
        <!--  <view class="time-text">{{item.year2}}</view>-->
        <!--</view>-->
        <view class="liunian-right text-center p-l-6 p-r-6" v-if="augurDate.liuNian">
          <view class="liunian-col">
            <view class="time-text" v-for="(item,index) in augurDate.liuNian.firstYear" :key="index">{{item}}</view>
          </view>
          <view class="liunian-col">
            <view class="" v-for="(item,index) in augurDate.liuNian.liuNianArr" :key="index">
              <view v-for="(citem, cindex) in item" :key="cindex">{{citem.ganzhi}}</view>
            </view>
          </view>
          <view class="liunian-col">
            <view class="time-text" v-for="(item,index) in augurDate.liuNian.lastYear" :key="index">{{item}}</view>
          </view>
        </view>
      </view>
    </view>
    <u-gap height="20"></u-gap>
    <!--v-if="newData.introduction"-->
    <view class="bg-white result-main p-30">
      <!--<view class="result-main_title">您的八字命局中性格如何?</view>-->
      <!--<view class="result-main_content">{{ userinfo.nickname }}{{userinfo.gender || ''}}你好, <text v-html="newData.introduction"></text></view>-->



      <!--<view class="result-main_title">你的八字命局中五行是否有缺?阴阳是否平衡?五行旺缺、喜忌对运势有哪些关键作用?如何调节五行，达到平衡?</view>-->
      <!--<view class="wuxing-content">-->
      <!--  <view class="wuxing-title">五行强弱</view>-->
      <!--  <view class="wuxing-list">-->
      <!--    <view class="wuxing-item" v-for="(item,index) in resultData.wuxing_rate" :key="index">-->
      <!--      <view class="wuxing-item_bg">-->
      <!--        <image :src="$image('/images/wuxing-bg' + item.icon + '.png')" mode="aspectFill"></image>-->
      <!--        <view class="wuxing-rate">10.3%</view>-->
      <!--      </view>-->
      <!--      <view class="wuxing-item_desc" :class="[typeColor(item.title)]">{{item.desc}}</view>-->
      <!--    </view>-->
      <!--  </view>-->
      <!--</view>-->
      <!--<view class="result-main_content">{{resultData.xingge}}</view>-->


      <!--<view v-for="(section, key) in newData" :key="key">-->
      <!--  <view class="result-main_title" v-if="section.title">{{ section.title.replace('### ', '') }}</view>-->
      <!--  <view v-if="Array.isArray(section.content)">-->
      <!--    <view class="result-main_content" v-for="(item, index) in section.content" :key="index" v-html="item"></view>-->
      <!--  </view>-->
      <!--</view>-->

      <view class="result-body" style="white-space: pre-line;" v-html="aiResultStr"></view>



      <!--<view class="result-main_title">你的八字命局中感情婚姻如何?</view>-->
      <!--<view class="result-main_content">{{resultData.xingge}}</view>-->
      <!--<view class="result-main_title">你八字命局的整体财运如何?什么时候要注意规避破财?怎么样增加财运，如何调整更吸财旺财留财守财?</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_title">你八字命局的整体事业发展如何?</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_title">你八字命局的整体健康如何?</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_title">你八字命局未来大运流年如何?</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_title">你八字命局在农历2024年1-12月每月的运程转机，变动提醒?</view>-->
      <!--<view class="result-main_content brown-color">{{resultData.zhuanji}}</view>-->
      <!--<view class="result-main_sub_title">丙寅月(2024.02.04-2024.03.04)</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_sub_title">丁卯月(2024.03.05-2024.04.03)</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_sub_title">戊辰月(2024.04.04-2024.05.04)</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
      <!--<view class="result-main_sub_title">己巳月(2024.05.05-2024.06.04)</view>-->
      <!--<view class="result-main_content">{{resultData.sub_content}}</view>-->
    </view>
    <!--<u-gap height="15"></u-gap>-->
    <!--<view class="bg-white summary-card p-30">-->
    <!--  <view class="summary-card_title">八字命局总结</view>-->
    <!--  <view class="summary-card_tag blue-tag">总结一</view>-->
    <!--  <view class="summary-card_content">你的八字其实无论是感情还是财运上的运势都不算差，但是因为命局中五行水为喜用且不够强旺，导致泄财泄运比较严重，婚姻感情比较容易发生重大转变情况，建议进行补旺五行水的调理，对你的格局来说比较关键。</view>-->
    <!--  <view class="summary-card_tag yellow-tag">总结二</view>-->
    <!--  <view class="summary-card_content">你的八字其实无论是感情还是财运上的运势都不算差，但是因为命局中五行水为喜用且不够强旺，导致泄财泄运比较严重，婚姻感情比较容易发生重大转变情况，建议进行补旺五行水的调理，对你的格局来说比较关键。</view>-->
    <!--  <view class="summary-card_tag red-tag">总结三</view>-->
    <!--  <view class="summary-card_content">你的八字其实无论是感情还是财运上的运势都不算差，但是因为命局中五行水为喜用且不够强旺，导致泄财泄运比较严重，婚姻感情比较容易发生重大转变情况，建议进行补旺五行水的调理，对你的格局来说比较关键。</view>-->
    <!--</view>-->
    <u-gap height="30"></u-gap>
  </view>
</template>

<script>
import {req_chat} from "@/common/api";

let app = getApp();
import resultData from '@/common/result.json';
import {getAiZangGan, getCalendarDate} from "@/common/utils";
import {agent_chat} from "@/common/mock";
export default {
  data() {
    return {
      timeList:['年柱','月柱','日柱','时柱'],
      // shishen:['偏财','偏印','元女','偏印'],
      // tiangan:['壬','丙','戊','丙'],
      // dizhih:['申','午','寅','辰'],
      // zanggan:[
      //   ['庚食神','壬偏财','戊比肩'],
      //   ['丁正印','己劫财'],
      //   ['甲七杀','丙偏印','戊比肩'],
      //   ['戊比肩','乙正官','癸正财'],
      // ],
      // shieryun:['病','帝旺','长生','冠带'],
      // kongwang:['戌亥','寅卯','申酉','子丑'],
      // nayin:['剑锋金','天河水','城墙土','沙中土'],
      // shensha:[
      //   ["文昌", "驿马", "日破", "天印贵人", "天厨贵人", "空亡"],
      //   ["羊刃", "将星", "阴角", "月德", "突然", "天狗", "德秀贵人", "吊客"],
      //   ["学堂", "天德合", "驿马", "破碎", "大耗", "德秀贵人", "阳差阳错"],
      //   ["红艳", "太极贵人", "月德", "血刃", "华盖", "白虎", "德秀贵人", "阴罗关", "童子煞"]
      // ],
      // resultData:resultData.data,
      userinfo: {},
      augurDate: {},
      // newData: {},
      agent_chat: JSON.parse(JSON.stringify(agent_chat)),
      aiResultStr: "", //ai回答
    };
  },
  onLoad(options) {
    // options.order_id &&
    if(options.userData){
      this.getUserInfo(options.order_id, options.userData);
    }else{
      this.getUserInfo();
    }
  },
  computed:{
    // 天干地址文字颜色
    typeColor(){
      return function(type) {
        let str = '';
        switch(type){
          case '金': //
            str = 'yellow-color';
            break
          case '木': //
            str = 'green-color';
            break
          case '水': //
            str = 'blue-color';
            break
          case '火': //
            str = 'red-color';
            break
          case '土': //
            str = 'brown-color';
            break
        }
        return str;
      };
    },
    zangganColor(){
      return function(title){
        let str = '';
        // 属木
        if(title == '庚' || title == '辛' || title == '申' || title == '酉'){
          str = 'yellow-color';
        }else if(title == '甲' || title == '乙' || title == '寅' || title == '卯'){
          str = 'green-color';
        }else if(title == '壬' || title == '癸' || title == '亥' || title == '子'){
          str = 'blue-color';
        }else if(title == '丙' || title == '丁' || title == '巳' || title == '午'){
          str = 'red-color';
        }else if(title == '戊' || title == '己' || title == '丑' || title == '辰' || title == '未' || title == '戌'){
          str = 'brown-color';
        }
        return str;
      }
    },
  },
  methods: {
    parseTextToObject(text) {
      const sections = text.split('\n\n'); // 根据段落分割
      const result = {}; // 存放最终结果
      let lastKey = null; // 记录最近的标题键名

      sections.forEach((section) => {
        const lines = section.split('\n').map((line) => line.trim()); // 分割成行并去掉首尾空格
        if (lines[0].startsWith('###') || lines[0].match(/^[^。\n]+：$/)) {
          // 如果是标题段落（支持###标题 或 标点符号标题）
          const key = lines[0].replace(/^#+\s*/, '').replace(/：$/, '').trim(); // 去掉符号并作为键名
          result[key] = {
            title: lines[0],
            content: lines.slice(1), // 存储内容部分
          };
          lastKey = key; // 更新最近的键名
        } else if (lastKey) {
          // 如果是没有标题的段落，归属到最近的标题
          result[lastKey].content.push(...lines);
        } else {
          // 如果是没有标题的首段内容，作为引言存储
          result.introduction = result.introduction
              ? result.introduction + '\n\n' + section.trim()
              : section.trim();
        }
      });

      return result;
    },
    async getUserInfo(order_id = null, userData = null){
      app.getMineInfo().then(async (res)=>{
        this.userinfo = res;
        app.isCompleteInfo();
        if(this.userinfo){
          let agent_type = 6;//6=流年运势, 7=详批八字
          if(userData){
            let birthdayData = JSON.parse(decodeURIComponent(userData));
            this.userinfo.birthday = birthdayData.birthday;
            this.userinfo.gender = birthdayData.gender;
            this.userinfo.nickname = birthdayData.nickname;
            agent_type = birthdayData.agent_type;
          }
          this.augurDate = await getCalendarDate({
            date:this.$u.timeFormat(this.userinfo.birthday, 'yyyy-mm-dd hh:MM:ss'),
            shenShaParams: {
              // isShenSha: true,
              day: this.$u.timeFormat(this.userinfo.birthday, "yyyy-mm-dd hh:MM:ss"),
              sex: this.userinfo.gender === "男" ? 1 : 0,
              name: this.userinfo.nickname,
            }
          });
          let {
            lunarDateChinese,
            shiShen,
            zangGan,
            naYin,
            kongWang,
            daYun,
            liuNian,
            jiShenXiongSha,
            aiZangGan
          } = this.augurDate;
          let bazi = {
            profile: {
              "姓名": this.userinfo.nickname,
              "性别": this.userinfo.gender,
            },
            "公历": this.$u.timeFormat(this.userinfo.birthday, "yyyy-mm-dd hh:MM:ss"),
            "农历": lunarDateChinese,
            "十神": shiShen,
            "纳音": naYin,
            "空亡": kongWang,
            "大运": daYun,
            "流年": liuNian.allData,
            "吉神凶煞": jiShenXiongSha,
            "藏干": aiZangGan,
          };
          let newBazi = Object.assign(this.agent_chat.bazi, bazi);
          this.agent_chat.birthday = this.$u.timeFormat(this.userinfo.birthday, 'yyyy-mm-dd');
          this.agent_chat.bazi = JSON.stringify(newBazi);
          if(this.augurDate.zangGan && this.augurDate.zangGan.length > 0){
            getAiZangGan(this.augurDate.zangGan);
            let data = {
              agent_type: Number(agent_type),
              query: agent_type == 6 ?"流年运势" : "详批八字",
              // isAi: true,
            };
            if(order_id){
              data.order_id = order_id;
            }
            this.agent_chat.request_time = this.$u.timeFormat(new Date().getTime(), "yyyy-mm-dd hh:MM:ss");
            const result = await this.$api.req_chat({...this.agent_chat, ...data});
            // if(result.result !== "" && result.status == "SUCCESS"){
            if(result.code == 1){
              console.log("查看流年运势回调", result);
              // 使用解析函数
              // this.newData = this.parseTextToObject(result.result);
              this.aiResultStr = result.data.result;
              console.log(this.aiResultStr);
            }
          }
        }
      });
    },
  },
}
</script>

<style lang="scss" scoped>
  .result-container{
    padding: 0 30rpx;
    color: #000;
    .result-top{
      height: 238rpx;
      position: relative;
      image{
        width: 100%;
        height: 100%;
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        border-radius: 20rpx;
      }
      .result-content{
        position: absolute;
        z-index: 1;
        color: #FFFFFF;
        font-size: 26rpx;
        padding: 30rpx;
        width: 100%;
        &_row{
          display: flex;
          align-items: center;
          justify-content: space-between;
          .result-top_title{

          }
          .result-top_value{
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding-left: 30rpx;
            flex: 1;
          }
        }
      }
    }
    .result-body{
      border-radius: 20rpx;
      overflow: hidden;
      font-size: 26rpx;
      &_cell:nth-child(odd){
        background: #DFE9FF !important;
      }
      &_cell:first-child{
        .result-body_cell_title{
          border-top-left-radius: 20rpx;
          border-top-right-radius: 20rpx;
        }
      }
      &_cell:last-child{
        .result-body_cell_title{
          border-bottom-left-radius: 20rpx;
          border-bottom-right-radius: 20rpx;
        }
      }
      &_cell {
        display: flex;
        align-items: stretch;
        margin-top: -2rpx;
        background: #FFFFFF;
        &_title{
          background: #2F67E1;
          width: 113rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          color: #FFFFFF;
        }
        &_value{
          flex: 1;
          display: flex;
          align-items: center;
          >view{
            width: calc(100% / 4);
            padding: 18rpx 0;
            text-align: center;
          }
        }
      }
      .liunian-right{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;
        .liunian-col{
          display: flex;
          >view{
            width: calc(100% / 8);
          }
          view{
            padding: 6rpx 0;
          }
        }
      }
    }
    .result-main{
      border-radius: 20rpx;
      &_title{
        color: #D8352A;
        font-size: 32rpx;
        font-family: 'pingfang-heavy';
        position: relative;
        padding-left: 20rpx;
        margin-bottom: 10rpx;
        line-height: 48rpx;
      }
      &_title:before{
        width: 6rpx;
        height: 24rpx;
        content: '';
        background: #D8352A;
        position: absolute;
        top: 12rpx;
        left: 0rpx;
        border-radius: 3rpx;
      }
      &_sub_title{
        padding-left: 54rpx;
        font-size: 28rpx;
        color: #D8352A;
        font-family: 'pingfang-heavy';
      }
      &_content{
        font-size: 26rpx;
        line-height: 48rpx;
        text-indent: 2em;
        margin-bottom: 10rpx;
      }
      .wuxing-content{
        background: #F5F6FA;
        padding: 30rpx 0;
        border-radius: 20rpx;
        margin-bottom: 20rpx;
        .wuxing-title{
          color: #fff;
          font-size: 28rpx;
          font-family: 'pingfang-heavy';
          text-align: center;
          padding: 10rpx 0;
          margin-bottom: 40rpx;
          background: linear-gradient(to right, #EEF0F9, #254AD5, #EEF0F9);
        }
        .wuxing-list{
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 0 44rpx;
          .wuxing-item{
            width: calc(100% / 5 - 16rpx);
            &_bg{
              height: 358rpx;
              width: 100%;
              position: relative;
              image{
                width: 100%;
                height: 100%;
                display: block;
              }
              .wuxing-rate{
                position: absolute;
                bottom: 16rpx;
                left: 50%;
                font-size: 24rpx;
                font-family: 'pingfang-heavy';
                font-weight: bold;
                transform: translateX(-50%);
              }
            }
            &_desc{
              font-size: 28rpx;
              font-family: 'pingfang-heavy';
              text-align: center;
              margin-top: 30rpx;
            }
          }
        }
      }
    }
    .summary-card{
      border-radius: 20rpx;
      &_title{
        color: #D8352A;
        font-size: 32rpx;
        font-family: 'pingfang-heavy';
        position: relative;
        padding-left: 20rpx;
        line-height: 48rpx;
      }
      &_title:before{
        width: 6rpx;
        height: 24rpx;
        content: '';
        background: #D8352A;
        position: absolute;
        top: 12rpx;
        left: 0rpx;
        border-radius: 3rpx;
      }
      &_tag{
        border-radius: 10rpx;
        color: #FFFFFF;
        font-size: 28rpx;
        font-family: 'pingfang-heavy';
        padding: 14rpx 40rpx;
        display: inline-block;
        margin: 30rpx 0;
      }
      .blue-tag{
        background: #2F67E1;
      }
      .yellow-tag{
        background: #F7B21D;
      }
      .red-tag{
        background: #D5242C;
      }
      &_content{
        font-size: 26rpx;
        line-height: 48rpx;
      }
    }
  }
</style>
